package com.jjwxc.jwjskandriod.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.activity.WebActivity;
import com.jjwxc.jwjskandriod.config.Url;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;

public class LawDialog extends Dialog implements
        View.OnClickListener {
    private Context context;

    private Button button_one;
    private Button button_two;
    private TextView tv_yhzcxy;
    private TextView tv_yszc;
    private DialogClickListener listener;
    public LawDialog(Context context,DialogClickListener dialogClickListener) {
        super(context);
        this.context = context;
        this.listener = dialogClickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_law);
        initDialog();
        button_one = findViewById(R.id.button_one);
        button_two = findViewById(R.id.button_two);
        tv_yszc = findViewById(R.id.tv_yszc);
        tv_yhzcxy = findViewById(R.id.tv_yhzcxy);
        button_two.setOnClickListener(this);
        button_one.setOnClickListener(this);
        tv_yhzcxy.setOnClickListener(this);
        tv_yszc.setOnClickListener(this);
    }

    /**
     * 设置dialog的宽为屏幕的3分之1
     */
    private void initDialog() {
        setCanceledOnTouchOutside(false);
        setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getRepeatCount() == 0) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = (int) (FFUtils.getDisWidth() / 8 * 7); // 设置宽度
        this.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.button_one) {
            dismiss();
            listener.onNoClick(this);
        } else if (v.getId() == R.id.button_two) {
            dismiss();
            listener.onOkClick(this);
        }else if (v.getId() == R.id.tv_yszc) {
            Intent intent = new Intent(context, WebActivity.class);
            intent.putExtra("WEB_URL", Url.privacy);
            intent.putExtra("WEB_NAME", "用户隐私条款");
           context.startActivity(intent);
        }else if (v.getId() == R.id.tv_yhzcxy) {
            Intent intent = new Intent( context, WebActivity.class);
            intent.putExtra("WEB_URL", Url.registerUser);
            intent.putExtra("WEB_NAME", "用户注册协议");
            context.startActivity(intent);
        }
    }


    public interface DialogClickListener {
        void onOkClick(Dialog dialog);

        void onNoClick(Dialog dialog);
    }
}
